using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;

namespace Gj
{
    public class AngleHelper : NodeHelper
    {
        public enum Axle
        {
            X,
            Y,
            Z,
        }
        
        public enum Type
        {
            One,
            Hundred,
            Value,
        }

        public Vector3 offset;
        public Type type = Type.Hundred;
        public Axle axle = Axle.Y;
        public BindDataType dataType = BindDataType.Float;
        protected override BindDataType bindDataType => dataType;

        public string[] values;

        protected override void BindData(int i)
        {
            switch (type)
            {
                case Type.Hundred:
                    SetAngle(i / (float)100 * 360);
                    break;
            }
        }

        protected override void BindData(float f)
        {
            switch (type)
            {
                case Type.One:
                    SetAngle(f * 360);
                    break;
            }
        }

        protected override void BindData(string s)
        {
            switch (type)
            {
                case Type.Value:
                    SetAngle(GetAngle(s));
                    break;
            }
        }

        float GetAngle(string s)
        {
            var index = 0;
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] == s)
                {
                    index = i;
                }
            }

            return MathTools.linear(0, 360, (float)index / (float)(values.Length - 1));
        }

        private void SetAngle(float angle)
        {
            switch (axle)
            {
                case Axle.X:
                    gameObject.transform.localRotation = Quaternion.Euler(new Vector3(angle, 0, 0) + offset);
                    break;
                case Axle.Y:
                    gameObject.transform.localRotation = Quaternion.Euler(new Vector3(0, angle, 0) + offset);
                    break;
                case Axle.Z:
                    gameObject.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, angle) + offset);
                    break;
            }
        }
    }
}